package com.example.myapplication.volunteerservice;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.myapplication.Config.OKHttpTool;
import com.example.myapplication.Config.ViewapteTool;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.VolunteerCarouselmapBean;
import com.example.myapplication.ShuJu.VolunteerNewslistBean;
import com.example.myapplication.apter.MsvieinNewslistApter;
import com.example.myapplication.apter.MsvieinformationlistApter;

import java.util.ArrayList;
import java.util.List;

public class MynewsFragment extends Fragment {


    private View view;
    private ImageView but_lawyerList;
    private ViewPager vp;
    private RecyclerView re;
    private Handler handler;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_volunteerservice, container, false);
        initview();
        //返回
        but_bloak();
        //轮播图
        Carouselmap();

        //新闻列表
        Newslist();
        return view;
    }

    private void Newslist() {
        OKHttpTool.doNetRequest("prod-api/api/volunteer-service/news/list",
                OKHttpTool.GET,
                VolunteerNewslistBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                VolunteerNewslistBean volunteerNewslistBean = (VolunteerNewslistBean)obj;
                                if(volunteerNewslistBean.getCode()==200){
                                    re.setLayoutManager(new LinearLayoutManager(MynewsFragment.this.getContext()));
                                    re.setAdapter(new MsvieinNewslistApter(MynewsFragment.this.getContext(),volunteerNewslistBean.getRows()));
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void Carouselmap() {
        OKHttpTool.doNetRequest("prod-api/api/volunteer-service/ad-banner/list",
                OKHttpTool.GET,
                VolunteerCarouselmapBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                VolunteerCarouselmapBean volunteerCarouselmapBean = (VolunteerCarouselmapBean)obj;
                                if(volunteerCarouselmapBean.getCode()==200){
                                    ArrayList<String> list = new ArrayList<>();
                                    List<VolunteerCarouselmapBean.DataBean> data = volunteerCarouselmapBean.getData();
                                    for (VolunteerCarouselmapBean.DataBean datum : data) {
                                        list.add(datum.getImgUrl());
                                    }
                                    ViewapteTool viewapteTool = new ViewapteTool(MynewsFragment.this.getContext(),list);
                                    vp.setAdapter(viewapteTool);
                                    handler.sendEmptyMessageDelayed(0,3*1000);
                                   
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void but_bloak() {
        but_lawyerList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
    }

    private void initview() {
        but_lawyerList = view.findViewById(R.id.but_lawyerList);
        vp = view.findViewById(R.id.vp);
        re = view.findViewById(R.id.re);
        handler = new Handler();
    }

    private Handler mhandler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if(msg.what==0){
                int currentItem = vp.getCurrentItem();
                currentItem = (currentItem+1)%3;
                vp.setCurrentItem(currentItem);
                mhandler.sendEmptyMessageDelayed(0,3*1000);
            }
        }
    };
}